package com.sduonline.isdapp.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class ConnectionPool {

	private static DataSource ds;// 之前非static，ds是只需要一个么
	private static ConnectionPool pool;

	private ConnectionPool() {
		if (ds == null) {
			ds = new ComboPooledDataSource();
		}
	}

	public static final ConnectionPool getInstance() {
		if (pool == null) {
			try {
				pool = new ConnectionPool();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return pool;
	}

	public synchronized final Connection getConnection() {
		try {
			return ds.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) {
		ConnectionPool pool = ConnectionPool.getInstance();
		Connection conn = pool.getConnection();
		try {
			String sql = "insert into isd_article(writer,content) values('sfg','adf')";
			PreparedStatement ptmt = conn.prepareStatement(sql);
			ptmt.execute(sql);
			ptmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}